<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 角色管理控制器
 * @package App\Http\Controllers\Admin
 */
class SysRoleController extends BaseController
{
    private $roleSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->roleSer = new \App\Services\System\Role();
    }

    /**
     * 角色列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $query = \App\Models\System\Role::query();
        $name = $request->input('name');
        if ($name){
            $query->where('name','like',"%".$name."%");
        }
        $displayName = $request->input('display_name');
        if ($displayName){
            $query->where('display_name', 'like', "%".$displayName."%");
        }
        $roleList = $query->get();
        $roleList = $roleList->isNotEmpty() ? $roleList->toArray() : [];
        if (empty($name) && empty($displayName)){
            $tree = new \App\Services\Tool\TreeService('role_id', 'parent_id', 'children');
            $tree->load($roleList);
            $treeRoleList = $tree->deepTree();
        }else{
            $treeRoleList = $roleList;
        }
        //权限数据
        $permSer = new \App\Services\System\Permission();
        $permList = $permSer->getCachePermList();
        //树结构权限列表列表
        $tree          = new \App\Services\Tool\TreeService('perm_id', 'parent_id', 'children');
        $tree->load($permList);
        $treePermList = $tree->deepTree();
        return $this->success('加载成功', ['roleList' => $treeRoleList, 'permList' => $treePermList]);
    }
    /**
     * 保存数据
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function save(Request $request)
    {
        $exceptId = (int) $request->input('role_id', 0);
        $this->validate($request,[
            'name' => 'required|unique:sys_roles,name,'.$exceptId.',role_id',
            'display_name' => 'required',
            'parent_id' => 'required',
        ]);
        $roleId = $request->input('role_id', 0);
        try {
            $rs = $this->roleSer->saveData($request->toArray());
            if (!$rs) {
                throw new \Exception(($roleId ? '编辑' : '添加') . '角色失败');
            }
            return $this->success(($roleId ? '编辑' : '添加') . '成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除角色
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $roleIds = $request->input('role_ids', []);
            if (!$roleIds) {
                throw new \Exception('请指定要删除的角色');
            }
            $rs = $this->roleSer->del($roleIds);
            if (!$rs) {
                throw new \Exception('删除失败');
            }
            return $this->success('删除成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
